package com.theten52.demo.thread;

/**
 * 测试{@link Thread#join()}和{@link Thread#sleep(long)}方法执行时线程的状态
 *
 * @author wangjin
 * @date 2021/3/6
 */
public class ThreadDemo {

    public static void main(String[] args) throws InterruptedException {
        Thread mainThread = Thread.currentThread();
        Runnable runnable = () -> {
            try {
                Thread.sleep(400);
                System.out.println("t1 self state;" + Thread.currentThread().getState());
                System.out.println("mainThread state;" + mainThread.getState());
                Thread.sleep(400);
                System.out.println("mainThread state;" + mainThread.getState());
                System.out.println("t1 self state;" + Thread.currentThread().getState());
                Thread.sleep(400);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };

        Thread t1 = new Thread(runnable);
        t1.start();

        Runnable watch = () -> {
            for (int i = 0; i < 15; i++) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("t1 state:" + t1.getState());
            }
        };
        Thread watchThread = new Thread(watch);
        watchThread.start();

        t1.join();
        watchThread.join();
    }
}
